<?php
// +----------------------------------------------------------------------
// | 有小象汇 自研正版源码 赋能开发者
// +----------------------------------------------------------------------
// | Copyright (c) 2017~2023 http://www.uxphp.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed 有小象汇并不是自由软件，未经许可不能去掉有小象汇相关版权
// +----------------------------------------------------------------------
// | Author:  有小象汇
// +----------------------------------------------------------------------

namespace app\services\car;


use app\dao\car\CarBookTypeDao;
use app\model\car\CarBookType;
use app\services\BaseServices;
use crmeb\exceptions\AdminException;
use crmeb\services\FormBuilder as Form;
use think\facade\Route as Url;

class CarBookTypeServices extends BaseServices
{
    public function __construct(CarBookTypeDao $dao)
    {
        $this->dao = $dao;
    }

    /**
     * 列表
     * @param array $where
     * @param bool $is_page
     * @return array
     */
    public function getList(array $where = [], bool $is_page = false){
        $page = $limit = 0;
        if($is_page){
            [$page,$limit] = $this->getPageValue();
        }
        $list = $this->dao->getList($where,$page,$limit);
        $type = [
            1 => '婚庆用车',
            2 => '短途接送',
            3 => '企业租车',
        ];
        foreach ($list['list'] as &$item){
            $item['type_name'] = $type[$item['type_id']];
        }
        return $list;
    }

    /**
     * 添加
     * @param int $id
     * @return array
     * @throws \FormBuilder\Exception\FormBuilderException
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function add(int $id){
        $info = $this->dao->get($id);
        $field = array();
        $title = $id ? '修改类型' : '添加类型';
        $field[] = Form::hidden('id', $info['id']??0);
        $field[] = Form::input('title', '车型名称', $info['title']??'');
//        $field[] = Form::radio('type_id','类型',$info['type_id']??1)->options([['value'=>1,'label'=>'婚庆用车'],['value'=>2,'label'=>'短途接送'],['value'=>3,'label'=>'企业租车']]);
        $field[] = Form::radio('type_id','类型',$info['type_id']??2)->options([['value'=>2,'label'=>'商务接送']]);
        $field[] = Form::number('sort','排序',$info['sort']??'');
        return create_form($title, $field, Url::buildUrl('/car/book/type/save'), 'POST');
    }

    /**
     * 保存
     * @param int $id
     * @param array $data
     * @return bool
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function save(int $id, array $data){
        $info = $this->dao->getOne(['title' => $data['title'],'is_del'=>0,'type_id'=>$data['type_id']]);
        if ($id) {
            if (!$this->dao->get($id)) throw new AdminException('数据不存在');
            if ($info && $id != $info['id']) throw new AdminException('已经存在');
            if ($this->dao->update($id, $data)) {
                return true;
            } else {
                throw new AdminException('修改失败或者您没有修改什么');
            }
        } else {
            unset($data['id']);
            $data['add_time'] = time();
            if ($info) throw new AdminException('已经存在');
            if ($this->dao->save($data)) {
                return true;
            } else {
                throw new AdminException('添加失败');
            }
        }
    }

    /**
     * 删除
     * @param int $id
     * @return string
     */
    public function delete(int $id){
        $res = $this->dao->update($id,['is_del'=>1,'delete_time'=>time()]);
        if(!$res) throw new AdminException('删除失败,请稍候再试!');
        return '删除成功!';
    }

    /**
     * 所有车辆类型列表
     * @return array
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function carBookType($data){
        $list = CarBookType::where(['is_del'=>0,'type_id'=>$data['type_id']])->select()->toArray();
        return $list;
    }
}
